﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;

namespace mesoBoard.Data.Repositories
{
    public class ForumRepository : IForumRepository
    {
        private mbEntities db;

        public ForumRepository(mbEntities mbDataContext)
        {
            this.db = mbDataContext;
        }

        public void SyncAllForums()
        {
            foreach (Forum f in db.Forums.AsEnumerable())
            {
                SyncForum(f.ForumID);
            }
        }

        public void SyncForum(int ForumID)
        {
            Forum TheForum = GetForum(ForumID);

            int TotalPosts = TheForum.Threads.Where(x=>x.Type != 4).Sum(x=>x.Posts.Count);
            int TotalThreads = TheForum.Threads.Count;

            Thread LastThread = TheForum.Threads.OrderByDescending(x => x.LastPost.Date).FirstOrDefault();

            if (LastThread == null)
                TheForum.LastPostID = 0;
            else
                TheForum.LastPostID = LastThread.Posts.OrderByDescending(x => x.Date).First().PostID;

            TheForum.TotalPosts = TotalPosts;
            TheForum.TotalThreads = TotalThreads;

            db.SaveChanges();
        }

        public bool HasNewPost(int ForumID, int UserID)
        {
            Forum TheForum = GetForum(ForumID);
            User TheUser = db.Users.First(x => x.UserID == UserID);
            ThreadRepository ThreadRep = new ThreadRepository(this.db);
            return TheForum.Threads.Any(x => ThreadRep.HasNewPost(x.ThreadID, UserID));
        }

        public IEnumerable<Forum> GetForums()
        {
            return db.Forums.AsEnumerable<Forum>();
        }

        public IEnumerable<Forum> GetForumsInCategory(int CatID)
        {
            return db.Forums.Where(x => x.CategoryID == CatID);
        }

        public Forum GetForum(int ForumID)
        {
            return db.Forums.First(x => x.ForumID == ForumID);
        }

        public int ThreadCount(int ForumID)
        {
            return GetForum(ForumID).Threads.Count;
        }

        public IEnumerable<ForumPermission> GetPermissions(int ForumID)
        {
            return db.ForumPermissions.Where(x => x.ForumID == ForumID);
        }

        public ForumPermission GetPermission(int ForumID, int RoleID)
        {
            return db.ForumPermissions.FirstOrDefault(x => x.ForumID == ForumID && x.RoleID == RoleID);
        }

        public void SubmitChanges()
        {
            db.SaveChanges();
        }

        public void UpdateForum(Forum TheForum)
        {
            if (TheForum.EntityKey == null)
                TheForum.EntityKey = ((Forum)db.GetObjectByKey(new System.Data.EntityKey("mbEntities.Forums", "ForumID", TheForum.ForumID))).EntityKey;

            db.Forums.ApplyCurrentValues(TheForum);
            db.SaveChanges();
        }

        public void CreateForum(Forum TheForum)
        {
            TheForum.Order = db.Forums.Count() > 0 ? db.Forums.OrderByDescending(x => x.Order).FirstOrDefault().Order + 1 : 1;
            db.Forums.AddObject(TheForum);
            db.SaveChanges();
        }

        public void DeleteForum(int ForumID)
        {
            db.Forums.DeleteObject(GetForum(ForumID));
            db.SaveChanges();
        }

        public void SetLastPostID(int ForumID, int PostID)
        {
            db.Forums.FirstOrDefault(x => x.ForumID == ForumID).LastPostID = PostID;
            db.SaveChanges();
        }
    }
}
